package com.baichang.taoshunda.controller;

import com.baichang.framework.controller.BaseController;
import com.baichang.framework.controller.IController;
import com.baichang.framework.controller.IPageController;
import com.baichang.framework.controller.WebBaseController;
import com.baichang.framework.excelption.ServiceException;
import com.baichang.framework.model.Page;
import com.baichang.taoshunda.ControllerConstants;
import com.baichang.taoshunda.entity.Params;
import com.baichang.taoshunda.service.ParamsService;
import com.baichang.taoshunda.service.ParamsTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by Administrator on 2017/8/22 0022.
 */


@Controller
@RequestMapping(ControllerConstants.PARAMSTYPE)
public class ParamsTypeController extends WebBaseController {
    @Autowired
    private ParamsTypeService paramTypeService;

    @Autowired
    private ParamsService paramsService;

    @RequestMapping("/findParameter")
    public String findParameter() {
        return "/taoshunda/paramstype/paramstype";
    }


    /**
     * 展示系统参数列表
     *
     * @return
     */
    @RequestMapping("/paramsType")
    public void paramsType(HttpServletRequest request, HttpServletResponse response) {
        initPage(request, response, new IPageController() {
            @Override
            public Page initPage(HttpServletRequest request, HttpServletResponse httpServletResponse) throws ServiceException {
                int typeId = getInt(request, "typeId", 0);
                String paramsName = getString(request, "paramsName", "");
                return paramTypeService.paramType(nowPage, pageSize, typeId, paramsName);
            }
        });
    }


    /**
     * 添加或修改参数、参数值
     *
     * @param request
     * @param response
     */
    @RequestMapping("/saveOrUpdate")
    public void saveParamTypeOrType(HttpServletRequest request, HttpServletResponse response) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest request, HttpServletResponse response) throws ServiceException {
                int id = getInt(request, "id", 0);
                int status = getInt(request, "status", 1);
                String name = getString(request, "name", "");
                String descri = getString(request, "descri", "");
                String value = getString(request, "value", "");
                Integer typeId = getInt(request, "typeId", 0);
                String remark = getString(request, "remark", "");
                return paramTypeService.saveOrUpdateParamType(id, typeId, name, status, value, descri, remark);
            }
        });
    }

    /**
     * 进行删除
     */
    @RequestMapping("/delete")
    public void delete(HttpServletRequest request, HttpServletResponse response) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest request, HttpServletResponse response) throws ServiceException {
                int id = getInt(request, "id", 0);
                Integer typeId = getInt(request, "typeId", 0);
                return paramTypeService.delete(id, typeId);
            }
        });
    }

    /**
     * 跳转关于我们
     *
     * @return
     */
    @RequestMapping("/findAboutIndex")
    public String findAbout(HttpServletRequest request, HttpServletResponse response, Model model) {
        Params params = paramsService.getParamsInfo();
        model.addAttribute("id", params.getId());
        model.addAttribute("name", params.getName());
        model.addAttribute("value", params.getValue());
        model.addAttribute("remark", params.getRemark());
        return "/taoshunda/about/aboutUser";
    }

    /**
     * 修改关于我们
     *
     * @param request
     * @param response
     */
    @RequestMapping("/updateParams")
    public void updateParams(final HttpServletRequest request, HttpServletResponse response) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                int id = getInt(request, "id", 0);
                String name = getString(request, "name", "");
                String value = getString(request, "value", "");
                String remark = getString(request, "remark", "");
                return paramsService.updateParams(id, name, value, remark);
            }
        });
    }
}
